package cn.iocoder.yudao.module.member.api.newsStudyInfo;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyInfoDTO;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyInfoPageDTO;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyRankingDTO;
import cn.iocoder.yudao.module.member.api.newsStudyInfo.dto.NewsStudyTypeDTO;
import cn.iocoder.yudao.module.member.controller.admin.newsStudyInfo.vo.NewsStudyInfoPageReqVO;
import cn.iocoder.yudao.module.member.convert.newsStudyInfo.NewsStudyInfoConvert;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyInfo.NewsStudyInfoDO;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyInfo.NewsStudyRankingDO;
import cn.iocoder.yudao.module.member.dal.dataobject.newsStudyType.NewsStudyTypeDO;
import cn.iocoder.yudao.module.member.service.newsStudyInfo.NewsStudyInfoService;
import cn.iocoder.yudao.module.member.service.newsStudyType.NewsStudyTypeService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 新闻(研究成果) API 接口
 *
 * @author 芋道源码
 */
@Service
public class NewsStudyInfoApiImpl implements NewsStudyInfoApi {
    @Resource
    private NewsStudyTypeService newsStudyTypeService;
    @Resource
    private NewsStudyInfoService newsStudyInfoService;

    @Override
    public List<NewsStudyTypeDTO> getNewsStudyTypeList(Integer type) {
        List<NewsStudyTypeDO> newsStudyTypeList = newsStudyTypeService.getNewsStudyTypeList(type);
        List<NewsStudyTypeDTO> list=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(newsStudyTypeList)){
            newsStudyTypeList.stream().forEach(m->{
                NewsStudyTypeDTO newsStudyTypeDTO=new NewsStudyTypeDTO();
                BeanUtils.copyProperties(m,newsStudyTypeDTO);
                list.add(newsStudyTypeDTO);
            });
        }
        return list;
    }

    @Override
    public PageResult<NewsStudyInfoDTO> getNewsStudyInfoPage(NewsStudyInfoPageDTO pageVO) {
        NewsStudyInfoPageReqVO pageReqVO=new NewsStudyInfoPageReqVO ();
        BeanUtils.copyProperties(pageVO,pageReqVO);
        PageResult<NewsStudyInfoDO> newsStudyInfoPage = newsStudyInfoService.getNewsStudyInfoPage(pageReqVO);
        return NewsStudyInfoConvert.INSTANCE.convertPage1(newsStudyInfoPage);
    }

    @Override
    public NewsStudyInfoDTO getNewsStudyInfoById(Long id) {
        NewsStudyInfoDO newsStudyInfo = newsStudyInfoService.getNewsStudyInfoById(id);
        if(null==newsStudyInfo){
            return null;
        }
        NewsStudyInfoDTO dto=new NewsStudyInfoDTO();
        BeanUtils.copyProperties(newsStudyInfo,dto);
        return dto;
    }

    @Override
    public void updateViewNum(Long id) {
        newsStudyInfoService.updateViewNum(id);
    }

    @Override
    public List<NewsStudyRankingDTO> getNewsStudyRanking(Integer type) {
        List<NewsStudyRankingDO> newsStudyRankingList = newsStudyInfoService.getNewsStudyRankingList(type);
        List<NewsStudyRankingDTO> list=new ArrayList<>();
        if(CollectionUtils.isNotEmpty(newsStudyRankingList)){
            newsStudyRankingList.stream().forEach(m->{
                NewsStudyRankingDTO rankingDTO=new NewsStudyRankingDTO();
                BeanUtils.copyProperties(m,rankingDTO);
                list.add(rankingDTO);
            });
        }
        return list;
    }
}
